'''
    参数解释：
    enir 环境（测试，正式）
    email 注册邮箱账号
    password 注册密码
    nativelang 母语数字代表（English 1 Chinese 2 Chinese_tw 3 Cantonese 4 Japanese 5 Korean 6 Spanish 7）
    learnlang1 学习语言数字代表（English 1 Chinese 2 Chinese_tw 3 Cantonese 4 Japanese 5 Korean 6 Spanish 7）
    nationality 国籍简称 (CN,KR,US,JP...)

    注册需要调用register_register函数，
    1 环境参数是必须填写的
    2 其他参数如果不填写，默认随机参数
    3 如果需要填写，按照下面的数据进行填写
    userData1 = {
                "nativelang" :2,
                "learnlang1" :3,
                "nationality" :"CN",
                "sex" : 0
            }
    4 register_register是单个注册，register_bath_register是批量注册
    5 如果提示需要手机验证，是因为后台开了手机注册的选项
    6 如果提示注册次数太多，需要到后台删除 ea894bcd43f678cc9922102d2730100839f0be04 设备
    '''
import requests, random, time, json

ENVIRONMENT_TEST = 0
ENVIRONMENT_PROD = 1

URLS = {
    ENVIRONMENT_TEST: "https://qtest.hellotalk8.com",
    ENVIRONMENT_PROD: "https://uploadprocn.hellotalk8.com"
}

REQUEST_PARAMS = {
    "client_lang": "English",
    "device_id": "ea894bcd43f678cc9922102d2730100839f0be04",
    "version": "5.2.40",
    "email_verify_code": "8888"
}
class Register_ht():

    def __init__(self, environmental=ENVIRONMENT_TEST):
        self.main_url = URLS[environmental]  # 0,测试环境，1 正式环境
        self.name_nick = str(time.time())[-1:-6:-1]


        # 获取国籍列表
        with open("Data.txt", "r") as file:
            str_data = file.read().split("=")[1].strip()
            str_data_json = json.loads(str_data)
            self.nationality = []
            for na in str_data_json:
                self.nationality.append(na)

    # 发送邮箱验证码
    def send_email_verify_code(self):
        url = f'{self.main_url}/user_register_center/v2/send_email_verify_code'
        payload = json.dumps({
            "client_lang": REQUEST_PARAMS['client_lang'],
            "email": self.name_nick + "@qq.com",
            "os_type": 0,
            "device_id": REQUEST_PARAMS['device_id'],
            "version": REQUEST_PARAMS['version']
        })
        response = requests.request("POST", url, data=payload)
        print(response.status_code)
        time.sleep(3)

    # verify_token
    def check_email_verify_code(self):
        url = f'{self.main_url}/user_register_center/v2/check'
        payload = json.dumps({
            "client_lang": REQUEST_PARAMS['client_lang'],
            "device_id": "ea894bcd43f678cc9922102d2730100839f0be04",
            "t": 1685960621154,
            "email_verify_code": REQUEST_PARAMS['email_verify_code'],
            "version": REQUEST_PARAMS['version'],
            "htntkey": "2a6b418fb5299d534e8047673ed7d2ea",
            "email": self.name_nick + "@qq.com",
            "password": "123456",
            "terminaltype": 0,
            "sim_country_code": "",
            "operator": "no operator",
            "login_type": 1
        })
        response = requests.request("POST", url, data=payload)
        verify_token = response.json()['data']['verify_token']
        #print(verify_token)
        return verify_token

    def register_user(self, user_data):
        if user_data is None:
            random_data = random.randint(1, 7)
            userData = {
                "nativelang": 1,  # random_data,
                "learnlang1": 2,  # random_data + 1
                # "nationality": random.choice(self.nationality),
                "nationality": "KR",
                "sex": random.choice([0, 1]),
                "birthday": "2010-06-03",
            }
        url = f'{self.main_url}/user_register_center/v2/register'

        data_json = {
            "nativelang": userData["nativelang"],
            "verify_token": self.check_email_verify_code(),
            "operator": "no operator",
            "sex": userData["sex"],
            "fullpy": self.name_nick.upper(),
            "shortpy": self.name_nick.upper(),
            "nationality": userData["nationality"],
            "nickname": self.name_nick,
            "headurl": "hu/200909/n47940387ef8a.jpg",
            "email": self.name_nick + "@qq.com",
            "learnlang1": userData["learnlang1"],
            "version": "5.2.40",
            "skilllevel1": 1,
            "birthday": "1998-09-17",
            "password": "123456",
            "root": 0,
            "allowed": 1,
            "sensor_distinct_id": "26150167-90C0-4099-980B-278BBC5D41F0",
            "terminaltype": 0,
            "bind_id": "bid@ht#s2fd-OOrheN0_DlK5EGWEnLaNXazl52_mRCC-fEpt839g9ankVisn-YVFQ6dVUoD",
            "appsflyer_id": "1685690854585-5740724",
            "network": "wifi",
            "deviceid": "ea894bcd43f678cc9922102d2730100839f0be04",
            "cnonce": "f4b5830058b6a505",
            "timezone48": 16,
            "device": "iPhone13,4",
            "t": 1685960644054,
            "sm_deviceid": "",
            "sim_country_code": "",
            "timezone": 8,
            "client_lang": REQUEST_PARAMS['client_lang'],
            "htntkey": "c7efd44081fa6a63ea0315af58dfff30",
            "login_type": 1,
            "watchman_token": "9ca16ae2e6eeb0f63a9a8af893e442a9968eb6d14b828b9aadd83c8b98afa3ec42948ff783e62af0febec3b940f0feacc3b92a829984b6e642e9b285d2d54a879a8ba6c14aa69fb8bbc6478d898f86f24f93b8ee9e"

        }

        respones = requests.post(url=url, json=data_json, verify=False)
        # print(url)
        respones_json = respones.json()
        # print(respones_json)
        if respones_json["status"] == 0:
            user_id = respones_json["data"]['user_id']
            userData_requests = {}
            userData_requests['国籍'] = userData['nationality']
            userData_requests['母语'] = userData['nativelang']
            userData_requests['性别'] = userData['sex']
            userData_requests['学习语言'] = userData['learnlang1']
            userData_requests['email'] = data_json['email']
            userData_requests['user_id'] = user_id
            return userData_requests
        else:
            return respones_json["msg"]

    def register_check(self, user_data=None):
        self.send_email_verify_code()
        return self.register_user(user_data)



# 随机注册
# print(Register_ht(0).register_register())
print(Register_ht(0).register_check())
